package com.cuckoo.cuckoo_app2.apiAsync.Imp;

import android.util.Log;

import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.PlatformApi;
import com.cuckoo.cuckoo_app2.config.ApiConfig;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.dao.UserPlatformDao;
import com.cuckoo.cuckoo_app2.utils.NetworkRequestHelperUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PlatformApiImp implements PlatformApi {
    private Gson gson;
    private ApiConfig apiConfig;

    public PlatformApiImp() {
        this.gson = new Gson();
        this.apiConfig = new ApiConfig();
    }

    @Override
    public void PlatformAddAsync(UserPlatformDao userPlatformDao, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserPlatform/platadd";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(userPlatformDao), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void PlatformEditAsync(Platform platform, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserPlatform/platedit";
        UserPlatformDao userPlatformDao = new UserPlatformDao();
        userPlatformDao.setPlatformId(platform.getId());
        userPlatformDao.setTitle(platform.getTitle());
        userPlatformDao.setImageUrl(platform.getImageUrl());
        userPlatformDao.setIsLogin(platform.getIsLogin());
        userPlatformDao.setAccount(platform.getAccount());
        userPlatformDao.setStart(platform.getStart());
        if(platform.getHasTips() != null){
            userPlatformDao.setHasTips(platform.getHasTips().toString());
        }
        userPlatformDao.setMessag(platform.getMessag());
        userPlatformDao.setToken(platform.getToken());
        userPlatformDao.setUserName(platform.getUserName());
        userPlatformDao.setUserPassord(platform.getUserPassord());
        userPlatformDao.setRetryCount(platform.getRetryCount());
        userPlatformDao.setParams(platform.getParams());
        if(platform.getTaskTime() != null){
            userPlatformDao.setTaskTime(platform.getTaskTime().intValue());
        }
        userPlatformDao.setCookie(platform.getCookie());
        userPlatformDao.setLocalStorage(platform.getLocalStorage());

        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(userPlatformDao), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void PlatformDeleteAsync(String id, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserPlatform/platedelete";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                body.put("platformId", id);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void PlatformListAsync(BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserPlatform/platlist";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "GET:" + http);
                try {
                    Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }
}
